package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.GoodsMapper;
import com.zhongyu.pojo.Goods;
import com.zhongyu.service.GoodsService;
import com.zhongyu.vo.GoodsVo;
import com.zhongyu.vo.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, GoodsVo goodsVo) {
        LambdaQueryWrapper<Goods> wrapper= Wrappers.<Goods>lambdaQuery();
        wrapper.eq(goodsVo.getProviderId()!=null, Goods::getProviderId,goodsVo.getProviderId())
                .like(!StringUtils.isEmpty(goodsVo.getName()), Goods::getName,goodsVo.getName())
                .like(!StringUtils.isEmpty(goodsVo.getCode()), Goods::getCode,goodsVo.getCode())
                .like(!StringUtils.isEmpty(goodsVo.getUnit()), Goods::getUnit,goodsVo.getUnit())
                .like(!StringUtils.isEmpty(goodsVo.getProducer()), Goods::getProducer,goodsVo.getProducer())
                .like(!StringUtils.isEmpty(goodsVo.getPdesc()), Goods::getPdesc,goodsVo.getPdesc())
                .like(!StringUtils.isEmpty(goodsVo.getTypeId()), Goods::getTypeId,goodsVo.getTypeId())
                .ge(goodsVo.getSpurchasePrice()!=null, Goods::getPurchasePrice,goodsVo.getSpurchasePrice())
                .le(goodsVo.getEpurchasePrice()!=null, Goods::getPurchasePrice,goodsVo.getEpurchasePrice())
                .ge(goodsVo.getSsellingPrice()!=null, Goods::getSellingPrice,goodsVo.getSsellingPrice())
                .le(goodsVo.getEsellingPrice()!=null, Goods::getSellingPrice,goodsVo.getEsellingPrice());
        Page page=new Page(pageNo,pageSize);
        List<Goods> list=goodsMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());

    }

    @Override
    public List<Goods> searchAll() {
     /*   QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("name","id");*/
        return goodsMapper.selectList(null);
    }

    @Override
    public List<Goods> searchAll(Integer providerId) {
        return goodsMapper.selectProviderId(providerId);
    }

    @Override
    public Goods searchCha(String name) {
        return goodsMapper.selectChaName(name);
    }

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public void del(Integer id) {
        goodsMapper.deleteById(id);

    }

    @Override
    public void update(Goods goods) {
        goodsMapper.updateById(goods);

    }

    @Override
    public List<Goods> findByDto(String providerId) {
        return goodsMapper.findByDto(providerId);
    }

    @Override
    public List<Goods> findByDt(String typeId) {
        return goodsMapper.findByDt(typeId);
    }

    @Override
    public Goods searchDui(Integer id) {
        return goodsMapper.searchDui(id);
    }


}
